package com.vogue.supplymall.order.domain.service.impl;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.account.domain.service.BaseCodeService;
import com.vogue.supplymall.match.domain.dto.BaseDTO;
import com.vogue.supplymall.match.domain.dto.PcrapDTO;
import com.vogue.supplymall.match.domain.dto.PcrapdDTO;
import com.vogue.supplymall.match.domain.dto.PfcrapDTO;
import com.vogue.supplymall.order.domain.dao.SupplierConsumerDao;
import com.vogue.supplymall.order.domain.model.SupplierConsumer;
import com.vogue.supplymall.order.domain.service.SupplierConsumerService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * The type Supplier consumer service.
 *
 * @author zhangliang
 * @date Create in 2018-08-03
 */
@Service("supplierConsumerService")
public class SupplierConsumerServiceImpl extends BaseServiceImpl<SupplierConsumer, Long> implements SupplierConsumerService {
    @Resource
    private SupplierConsumerDao supplierConsumerDao;
    @Resource
	private BaseCodeService baseCodeService;

    private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * Instantiates a new Supplier consumer service.
     *
     * @param baseDao the base dao
     */
    public SupplierConsumerServiceImpl(BaseDao<SupplierConsumer, Long> baseDao) {
        super(baseDao);
    }
    
	@Override
	public Map<String, Object> saveCustomerRelationAndPayMode(BaseDTO<PfcrapDTO> baseDTO) {
		Map<String, Object> resultMap=new HashMap<>();
		boolean resultFlag=true;
		String message="";
		
		Long sellSyscode=baseDTO.getRegistercode();	//卖家注册号
		Long sellSyscodeId =baseCodeService.checkSysCodeValidity(sellSyscode);	//卖家注册号id
		Long buySyscodeId=0L;
		
		PfcrapDTO pfcrDTO=baseDTO.getData();
		List<PcrapDTO> pcrList=pfcrDTO.getBuyerInfo();
		
		if(pcrList.size()>0) {
			for(PcrapDTO pcrDTO:pcrList) {
				
				String buySyscode=pcrDTO.getBuysyscode();
				if(StringUtils.isNotBlank(buySyscode)) {
					buySyscodeId =baseCodeService.checkSysCodeValidity(Long.valueOf(buySyscode));	//买家注册号id
					if(buySyscodeId==0||buySyscode.startsWith("0")) {
						resultFlag=false;
						message+="买家注册号"+buySyscode+"在商城中不存在，请检查！；</br>";
						continue;
					}
				}
				
				if(resultFlag) {
					//根据卖家注册号id和买家注册号Id得到买家门店信息
					List<Map<String, Object>> buyerInfoList=supplierConsumerDao.getBuyerInfoBySyscodeId(sellSyscodeId,buySyscodeId);
					
					List<PcrapdDTO> pcrdList=pcrDTO.getDetail();
					
						if(pcrdList.size()>0) {
							for(PcrapdDTO pcrdDTO:pcrdList) {	
								boolean editFlag=false;							//更新标记
								String id="";
								 String organId=pcrdDTO.getBuyorganid();		//买家门店id
								 String matchId=pcrdDTO.getBuymatchid();		//买家门店对应的供应链id
								 int payMode=pcrdDTO.getPaymode();				//买家门店支付方式
								
								 //判断门店是新增还是更新，对应不同的操作
								 for(Map<String, Object> map:buyerInfoList) {
									 id=map.get("id").toString();
									 String boId=map.get("buyorganid").toString();
									 
									 if(organId.equals(boId)) {
										 editFlag=true;
										 break;
									 }
									 
								 }
								 
								 //更新或者新增客户关系和支付方式
								 if(editFlag) {
									 String date=sdf.format(new Date());
									 Long eId=Long.valueOf(id);
									 
									 supplierConsumerDao.updatePayMode(eId, payMode, date);
								 }else {
									 SupplierConsumer sc=new SupplierConsumer();
									 sc.setBuymatchid(matchId);
									 sc.setBuyorganid(Long.valueOf(organId));
									 sc.setBuysyscodeid(buySyscodeId);
									 sc.setCreateDate(new Date());
									 sc.setModifyDate(new Date());
									 sc.setPaymode(payMode);
									 sc.setSellsyscodeid(sellSyscodeId);
									 
									 supplierConsumerDao.save(sc);
								 }
								 
							}
						}
				}
			}
		}
		
		resultMap.put("resultFlag", resultFlag);
		resultMap.put("message", message);
		
		return resultMap;
		
	}
}
